﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;

namespace UNSWThink
{
   /// <summary>
   /// Testing form. Used to test how to use controls without using any other production form
   /// </summary>
   public partial class Form1 : Form
   {
      /// <summary>
      /// Default constructor
      /// </summary>
      public Form1()
      {
         InitializeComponent();

         //pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
      }

      /// <summary>
      /// Set dataGridView1's data source property
      /// </summary>
      public DataTable DataSource
      {
         set
         {
            dataGridView1.DataSource = value;
         }
      }

      private void Form1_Load(object sender, EventArgs e)
      {
         this.Show();
         /*DataSet instructorData = new DataSet();
         DataTable responseData = new DataTable();
         DataTable enquiryData = new DataTable();
         DBConnection dbConn = new DBConnection("127.0.0.1", "5432", "unswthinkadmin", "password", "unswthink");
         //DBConnection dbConn = new DBConnection();
         bool connected = dbConn.Connect();

         if (!connected)
         {
            MessageBox.Show("Could not establish a connection to the DB server.", "No connection");
            this.Close();
         }
         else
         {
            MessageBox.Show("Commencing data retrieval", "Connection Established");
            instructorData = dbConn.GetAllInstructorRelatedData(1);
            dbConn.GetAllResponses(0, 1, responseData);
            dbConn.GetAllEnquiries(0, 1, enquiryData);
         }

         string stringTimestamp = HelperMethods.DateValuesToString(2000, 2, 30, 0, 0, 0);
         DateTime timestamp;
         if (!DateTime.TryParse(stringTimestamp, out timestamp))
         {
            MessageBox.Show("invalid date time (invalid date)");
         }

         stringTimestamp = HelperMethods.DateValuesToString(0, 0, 0, 0, 0, 0);
         if (!DateTime.TryParse(stringTimestamp, out timestamp))
         {
            MessageBox.Show("invalid date time (zeroes)");
         }

         stringTimestamp = HelperMethods.DateValuesToString(-1, -1, -1, -1, -1, -1);
         if (!DateTime.TryParse(stringTimestamp, out timestamp))
         {
            MessageBox.Show("invalid date time (negative)");
         }

         stringTimestamp = HelperMethods.DateValuesToString(999999, 99, 99, 99, 99, 99);
         if (!DateTime.TryParse(stringTimestamp, out timestamp))
         {
            MessageBox.Show("invalid date time (positive/out of range)");
         }

         stringTimestamp = HelperMethods.DateValuesToString(2000, 1, 1, 1, 1, 0);
         if (!DateTime.TryParse(stringTimestamp, out timestamp))
         {
            if (timestamp.ToString("dd/MM/yyyy HH:mm:ss") != stringTimestamp)
            {
               MessageBox.Show("stringTimeStamp not generated correctly (single digits)");
            }
         }
         else
         {
            Console.WriteLine(timestamp.ToString("dd/MM/yyyy HH:mm:ss"));
         }

         stringTimestamp = HelperMethods.DateValuesToString(2000, 10, 10, 10, 10, 0);
         if (!DateTime.TryParse(stringTimestamp, out timestamp))
         {
            if (timestamp.ToString("dd/MM/yyyy HH:mm:ss") != stringTimestamp)
            {
               MessageBox.Show("stringTimeStamp not generated correctly (double digits)");
            }
         }
         else
         {
            Console.WriteLine(timestamp.ToString("dd/MM/yyyy HH:mm:ss"));
         }*/

         /*string[] columnNames = {"account","class"};
         object[] values = {1,3};

         int result = 0;
         result = dbConn.InsertRecord("Teaches", columnNames, values);
         MessageBox.Show(result.ToString());
         result = dbConn.InsertRecord("Teaches", columnNames, values);
         MessageBox.Show(result.ToString());*/

         /*foreach (DataTable table in instructorData.Tables)
         {
            dataGridView1.DataSource = table;
            dataGridView1.Refresh();

            MessageBox.Show("Displaying table: " + table.TableName);
         }*/

         /*dataGridView1.DataSource = responseData.Tables["Response"];
         dataGridView1.Refresh();

         MessageBox.Show("Displaying table: " + responseData.Tables["Response"].TableName);

         while (true)
         {
            Thread.Sleep(10000);
            //int maxID = HelperMethods.GetMaxID(responseData.Tables["Response"]);
            responseData = dbConn.GetAllResponses(responseData.Tables["Response"].Rows.Count, 1, responseData);
            dataGridView1.DataSource = responseData.Tables["Response"];
            dataGridView1.Refresh();
            MessageBox.Show("Displaying table: " + responseData.Tables["Response"].TableName);
         }*/

         /*dataGridView1.DataSource = enquiryData.Tables["Enquiry"];
         dataGridView1.Refresh();

         MessageBox.Show("Displaying table: " + enquiryData.Tables["Enquiry"].TableName);

         while (true)
         {
            Thread.Sleep(10000);
            //int maxID = HelperMethods.GetMaxID(responseData.Tables["Response"]);
            enquiryData = dbConn.GetAllEnquiries(enquiryData.Tables["Enquiry"].Rows.Count, 1, enquiryData);
            dataGridView1.DataSource = enquiryData.Tables["Enquiry"];
            dataGridView1.Refresh();
            MessageBox.Show("Displaying table: " + enquiryData.Tables["Enquiry"].TableName);
         }*/
         //button1.Click += new EventHandler(button1_Click);
         /*tabControl1.MouseDown += new MouseEventHandler(tabControl1_MouseDown);
         tabControl1.MouseMove += new MouseEventHandler(tabControl1_MouseMove);
         tabControl1.MouseUp += new MouseEventHandler(tabControl1_MouseUp);*/
      }

      /*void tabControl1_MouseMove(object sender, MouseEventArgs e)
      {
         if (isMouseDown)
         {
            pictureBox1.BringToFront();
            pictureBox1.Location = new Point(e.X-offsetX, e.Y-offsetY-20);
            pictureBox1.Refresh();
            //offsetX = e.X;
            //offsetY = e.Y;
            alphablend = 0;
         }
      }

      void tabControl1_MouseUp(object sender, MouseEventArgs e)
      {
         isMouseDown = false;
         
         pictureBox1.Visible = false;
      }

      private void tabControl1_MouseDown(object sender, MouseEventArgs e)
      {
         isMouseDown = true;
         
         myBitmap = new Bitmap(tabControl1.Size.Width, tabControl1.Size.Height);
         //captureX = this.Location.X + (this.Size.Width - tabControl1.Location.X);
         //captureY = this.Location.Y + (this.Size.Height - tabControl1.Location.Y);
         captureX = tabControl1.Location.X;
         captureY = tabControl1.Location.Y;
         offsetX = e.X;
         offsetY = e.Y;
         tabControl1.SelectedTab.Show();
         tabControl1.SelectedTab.DrawToBitmap(myBitmap, new Rectangle(0, 0, tabControl1.Size.Width, tabControl1.Size.Height));
         alphablend = 100;
         Console.WriteLine(tabControl1.SelectedTab.Name);

         pictureBox1.Visible = true;
      }

      Graphics g;
      int captureX;
      int captureY;
      public int offsetX = 0;
      public int offsetY = 0;
      public bool isMouseDown = false;
      public int alphablend = 0;
      public Bitmap myBitmap;*/

      /*private void button1_Click(object sender, EventArgs e)
      {
         alphablend = 0;
         pictureBox1.Visible = true;
         myBitmap = new Bitmap(tabControl1.Width, tabControl1.Height);
         while (alphablend <= 246)
         {
            tabControl1.DrawToBitmap(myBitmap, new Rectangle(0, 0, tabControl1.Width, tabControl1.Height));
            alphablend = alphablend + 10;
            pictureBox1.Refresh();//this calls the paint action
         }
         if (tabControl1.SelectedTab.Name == "tabPage1")
         {
            tabControl1.SelectTab("tabPage2");
         }
         else
         {
            tabControl1.SelectTab("tabPage1");
         }
         while (alphablend >= 0)
         {
            tabControl1.DrawToBitmap(myBitmap, new Rectangle(0, 0, tabControl1.Width, tabControl1.Height));
            alphablend = alphablend - 10;
            pictureBox1.Refresh();//this calls the paint action
         }
         pictureBox1.Visible = false;
      }*/

      /*private void pictureBox1_Paint(object sender, PaintEventArgs e)
      {

         if (isMouseDown)
         {
            Graphics bitmapGraphics = Graphics.FromImage(myBitmap);

            Color backColour = this.BackColor;
            Color alphaColour = Color.FromArgb(alphablend, Color.Transparent);
            SolidBrush greyBrush = new SolidBrush(alphaColour);

            bitmapGraphics.CompositingMode = CompositingMode.SourceOver;

            bitmapGraphics.FillRectangle(greyBrush, new Rectangle(0, 0, tabControl1.Size.Width, tabControl1.Size.Height));

            e.Graphics.CompositingQuality = CompositingQuality.GammaCorrected;
         }

         e.Graphics.DrawImage(myBitmap, 0, 0);
      }*/
   }
}
